package com.example.demo.controller;

import com.example.demo.entity.Book;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("")
    public String bookPage() {
        return ("book");
    }

    @RequestMapping("postBook")
    @ResponseBody
    public Book postBook(String book_name, String book_author, String book_publisher, String book_type, String book_price,
                         String book_degree, String book_description, String seller_id, @RequestBody String book_image) {
        String book_id = bookService.postBook(book_name, book_author, book_publisher, book_type, book_price,
                book_degree, book_description, book_image, seller_id);
        return bookService.selectBookById(book_id);
    }

    @RequestMapping("selectBookById")
    @ResponseBody
    public Book selectBookById(String book_id) {
        return bookService.selectBookById(book_id);
    }

    @RequestMapping("selectBookByInfo")
    @ResponseBody
    public ArrayList<Book> selectBookByInfo(String book_name, String book_type, String order_type) {
        return bookService.selectBookByInfo(book_name, book_type, order_type);
    }

    @RequestMapping("tradeBook")
    @ResponseBody
    public int tradeBook(String book_id, String buyer_id) {
        return bookService.tradeBook(book_id, buyer_id);
    }

    @RequestMapping("accomplishTrade")
    @ResponseBody
    public int accomplishTrade(String book_id) {
        return bookService.accomplishTrade(book_id);
    }

    @RequestMapping("selectBookByUser")
    @ResponseBody
    public ArrayList<Book> selectBookByUser(String user_id, String select_type) {
        return bookService.selectBookByUser(user_id, select_type);
    }

}
